﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;


using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsNewsReleaseTableAdapters;
using ConstructionBidOnline.Common;


namespace ConstructionBidOnline.Biz
{
   public class NewsRelease:BOBase
    {

       #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected NewsRelease() { }

        #endregion

       #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the NewsRelease.
        /// </summary>
        public int NewsReleaseID
        {
            get { return _newsreleaseID; }
            set
            {
                if (_newsreleaseID != value)
                {
                    if (OnPropertyChanging("NewsReleaseID", _newsreleaseID, value))
                    {
                        int oldValue = _newsreleaseID;
                        _newsreleaseID = value;

                        OnPropertyChanged("NewsReleaseID", oldValue, value);
                    }
                }
            }
        }  private int _newsreleaseID = -1;

        /// <summary>
        /// News Release itle Value.
        /// </summary>
        public string NewsReleaseTitle
        {
            get { return _newsreleasetitle; }
            set
            {
                if (_newsreleasetitle != value)
                {
                    if (OnPropertyChanging("NewsReleaseTitle", _newsreleasetitle, value))
                    {
                        string oldValue = _newsreleasetitle;
                        _newsreleasetitle = value;
                        OnPropertyChanged("NewsReleaseTitle", oldValue, value);
                    }
                }
            }
        }  private string _newsreleasetitle = null;


        /// <summary>
        /// News
        /// </summary>
        public string News
        {
            get { return _news; }
            set
            {
                if (_news != value)
                {
                    if (OnPropertyChanging("News", _news, value))
                    {
                        string oldValue = _news;
                        _news = value;
                        OnPropertyChanged("News", oldValue, value);
                    }
                }
            }
        }  private string _news = null;




        /// <summary>
        /// Image File  Value.
        /// </summary>
        public string ImageFile
        {
            get { return _imagefile; }
            set
            {
                if (_imagefile != value)
                {
                    if (OnPropertyChanging("ImageFile", _imagefile, value))
                    {
                        string oldValue = _imagefile;
                        _imagefile = value;
                        OnPropertyChanged("ImageFile", oldValue, value);
                    }
                }
            }
        }  private string _imagefile = null;






        /// <summary>
        /// Video File  Value.
        /// </summary>
        public string VideoFile
        {
            get { return _videofile; }
            set
            {
                if (_videofile != value)
                {
                    if (OnPropertyChanging("VideoFile", _videofile, value))
                    {
                        string oldValue = _videofile;
                        _videofile = value;
                        OnPropertyChanged("VideoFile", oldValue, value);
                    }
                }
            }
        }  private string _videofile = null;




        /// <summary>
        /// Date/time at which the NewsRelease was created.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addeddate; }
            set
            {
                if (_addeddate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addeddate, value))
                    {
                        DateTime? oldValue = _addeddate;
                        _addeddate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addeddate = null;



        /// <summary>
        /// Date/time at which the NewsRelease was updated.
        /// </summary>
        public DateTime? UpdatedDate
        {
            get { return _updateddate; }
            set
            {
                if (_updateddate != value)
                {
                    if (OnPropertyChanging("UpdatedDate", _updateddate, value))
                    {
                        DateTime? oldValue = _updateddate;
                        _updateddate = value;
                        OnPropertyChanged("UpdatedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _updateddate = null;


        /// <summary>
        /// True iff the Admin has made this NewsRelease Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;

        #endregion

        #endregion

       #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty NewsRelease object.
        /// </summary>
        public static NewsRelease CreateNew()
        {
            NewsRelease obj = new NewsRelease();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.AddedDate = DateTime.Now;
            obj.UpdatedDate = DateTime.Now;
            obj.IsActive = false;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static NewsRelease CreateNew(NewsReleaseCollection collection)
        {
            NewsRelease obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (NewsRelease newsrelease in collection)
            {
                if (newsrelease.NewsReleaseID < minVal)
                    minVal = newsrelease.NewsReleaseID;
            }
            obj.NewsReleaseID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllNewsRelease

        /// <summary>
        /// Returns a collection of all NewsRelease.
        /// </summary>
        public static NewsReleaseCollection GetAllNewsRelease()
        {
            NewsReleaseTableAdapter adapter = new NewsReleaseTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsNewsRelease.NewsReleaseDataTable tbl = adapter.GetData(null);

            NewsReleaseCollection list = new NewsReleaseCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsNewsRelease.NewsReleaseRow row in tbl.Rows)
                {
                    NewsRelease newsrelease = new NewsRelease();
                    RelationalToObject(newsrelease, row);
                    list.Add(newsrelease);
                }
            }

            return (list);
        }

        #endregion

        #region GetNewsReleaseByID

        /// <summary>
        /// Returns an NewsRelease object for the specified 'NewsReleaseID' or null if no NewsRelease with the specified parameter(s) exists.
        /// </summary>
        public static NewsRelease GetNewsReleaseByID(int newsreleaseid)
        {
            NewsReleaseTableAdapter adapter = new NewsReleaseTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsNewsRelease.NewsReleaseDataTable tbl = adapter.GetData(newsreleaseid);

            if (tbl.Rows.Count == 1)
            {
                NewsRelease bc = new NewsRelease();
                RelationalToObject(bc, tbl.Rows[0]);
                return (bc);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'ID' returned more than one User record.");

            return (null);
        }

        #endregion

        #region GetNewsReleaseByNewsReleaseTitle

        /// <summary>
        /// Returns an NewsRelease object for the specified 'NewsReleaseTitle' or null if no NewsRelease with the specified parameter(s) exists.
        /// </summary>
        public static NewsRelease GetNewsReleaseByNewsReleaseTitle(string newsreleasetitle)
        {
            NewsReleaseTableAdapter adapter = new NewsReleaseTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsNewsRelease.NewsReleaseDataTable tbl = adapter.GetDataByNewsReleaseTitle(newsreleasetitle);

            if (tbl.Rows.Count == 1)
            {
                NewsRelease bc = new NewsRelease();
                RelationalToObject(bc, tbl.Rows[0]);
                return (bc);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'NewsReleaseTitle' returned more than one NewsRelease.");

            return (null);
        }

        #endregion

        #region GetNewsReleaseByIsActive

        /// <summary>
        /// Returns an NewsRelease object for the specified 'IsActive' or null if no NewsRelease with the specified parameter(s) exists.
        /// </summary>
        public static NewsRelease GetNewsReleaseByIsActive(bool isactive)
        {
            NewsReleaseTableAdapter adapter = new NewsReleaseTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsNewsRelease.NewsReleaseDataTable tbl = adapter.GetDataByIsActive(isactive);

            if (tbl.Rows.Count == 1)
            {
                NewsRelease bc = new NewsRelease();
                RelationalToObject(bc, tbl.Rows[0]);
                return (bc);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'IsActive' returned more than one NewsRelease.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified NewsRelease.
        /// </summary>
        public static void Update(NewsRelease obj)
        {
            bool updateNewsRelease = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateNewsRelease = (obj.ObjState != ObjectState.Unchanged);

                if (!updateNewsRelease)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsNewsRelease.NewsReleaseDataTable tbl = null;
            if (updateNewsRelease)
            {

                NewsReleaseTableAdapter adapter = new NewsReleaseTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsNewsRelease.NewsReleaseDataTable();
                dsNewsRelease.NewsReleaseRow row = tbl.NewNewsReleaseRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (NewsReleaseID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("NewsReleaseID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // BusinessName is required.
                if ((NewsReleaseTitle == null) || (NewsReleaseTitle.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("NewsReleaseTitle", "NewsRelease is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("NewsRelease: NSID={0}, NewsReleaseTitle={1}, {2}", NewsReleaseID, NewsReleaseTitle, News));
        }

        #endregion

        #endregion


    }


   public class NewsReleaseCollection : List<NewsRelease>
   {

       #region Types

       public class UserComparer : IComparer<NewsRelease>
       {
           public int Compare(NewsRelease user1, NewsRelease user2)
           {
               return (String.Compare(user1.NewsReleaseTitle, user2.NewsReleaseTitle));
           }
       }

       #endregion

       #region Public Methods

       #region FindByNewsReleaseID

       public NewsRelease FindByNewsReleaseID(int newsreleaseID)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].NewsReleaseID == newsreleaseID)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       

       #endregion

   }
}
